package com.labraff.redvideo;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class DetailFragment extends Fragment {
    TextView title,showSource,publishTime,summary;
    VideoView videoView;
    RatingBar ratingBar;
    Button playBtn,pauseBtn;
    public news newsItem = null;
    boolean isPad = false;
    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_detail, container, false);
        title = (TextView) view.findViewById(R.id.news_title);
        showSource = (TextView) view.findViewById(R.id.news_showSource);
        publishTime =(TextView) view.findViewById(R.id.news_publishTime);
        summary = (TextView) view.findViewById(R.id.news_summary);
        videoView = (VideoView) view.findViewById(R.id.news_video);
        ratingBar = (RatingBar) view.findViewById(R.id.ratingBar);
        playBtn = (Button) view.findViewById(R.id.playBtn);
        pauseBtn = (Button) view.findViewById(R.id.pauseBtn);

        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                String index = "<id-"+newsItem.getId()+">";
                Log.i("Rating index-value", index+rating);
                ratingBarEdit(index,rating);
            }
        });

        playBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isPad){
                    MediaController controller = new MediaController((MainActivity)getActivity());
                    controller.setMediaPlayer(videoView);
                    videoView.setMediaController(controller);
                    videoView.start();
                }else {
                    MediaController controller = new MediaController((detail)getActivity());
                    controller.setMediaPlayer(videoView);
                    videoView.setMediaController(controller);
                    videoView.start();
                }
            }
        });

        pauseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isPad){
                    MediaController controller = new MediaController((MainActivity)getActivity());
                    controller.setMediaPlayer(videoView);
                    videoView.setMediaController(controller);
                    videoView.pause();
                }else {
                    MediaController controller = new MediaController((detail)getActivity());
                    controller.setMediaPlayer(videoView);
                    videoView.setMediaController(controller);
                    videoView.pause();
                }
            }
        });

        return view;
    }

    public void setData(news newsItem,boolean isPad){
        this.isPad = isPad;
        this.newsItem = newsItem;
        title.setText(newsItem.getTitle());
        showSource.setText(newsItem.getShowSource());
        publishTime.setText(newsItem.getPublishTime());
        summary.setText(newsItem.getSummary());
        SharedPreferences SPF = getActivity().getSharedPreferences("RatingData",Context.MODE_PRIVATE);
        String key = "<id-"+newsItem.getId()+">";
        float rating = SPF.getFloat(key,5);
        ratingBar.setRating(rating);
        if(isPad){
            MediaController controller = new MediaController((MainActivity)getActivity());
            videoView.setVideoURI(Uri.parse(newsItem.getVideoSrc()));
            controller.setMediaPlayer(videoView);
            videoView.setMediaController(controller);
            videoView.start();
        }else {
            MediaController controller = new MediaController((detail)getActivity());
            videoView.setVideoURI(Uri.parse(newsItem.getVideoSrc()));
            controller.setMediaPlayer(videoView);
            videoView.setMediaController(controller);
            videoView.start();
        }
    }
    void ratingBarEdit(String index,float value){
        SharedPreferences SPF = getActivity().getSharedPreferences("RatingData",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = SPF.edit();
        editor.putFloat(index,value);
        editor.commit();
    }

}